#include "stdafx.h"
#include "GameStateMgr.h"
#include "DBDataMgr.h"
#include "PatrolState.h"


initialiseSingleton(CGameStateMgr);
CGameStateMgr::CGameStateMgr()
{
}

// 
bool CGameStateMgr::Init()
{
    IGameStateBase* pTmp = NULL;
    pCur=pLast=NULL;
    m_GameState.clear();
    switch (GET_DB().m_dwPatrolType)
    {
    case ENUM_LINE_PATROL:
    case ENUM_FB_PATROL:
    case ENUM_FB_WAIT:
        {
            pTmp = new CPatrolState;
        }
        break;
    case ENUM_CIRLE_PATROL:
        {
            pTmp = new CCirclePatrol;
        }
        break;
    case ENUM_RANDOM_PATROL:
        {
            pTmp = new CRandomPatrol;
        }
        break;
    case ENUM_STAND_PATROL:
        break;
    case ENUM_FISH_PATROL:

        break;

    }
    
    m_GameState.push_back(pTmp);
    return true;
}

bool CGameStateMgr::Run()
{
    VecGameState::iterator itr = m_GameState.begin();
    for (; itr!=m_GameState.end();)
    {
        pCur = *itr;

        if (!pCur)
        {
            return false;
        }
        //New state coming
        if (pCur!=pLast)
        {   
            pLast = pCur;
            pCur->Reset();
        }

        pCur->DoAction();

        bool bDel = pCur->NeedDel();
        if (bDel)
        {
            itr = m_GameState.erase(itr);
        }
        else
        {
            break;
        }
    }


    return true;

}

// release all the state object
bool CGameStateMgr::Destory()
{
    for (DWORD dd=0; dd<m_GameState.size(); ++dd)
    {
        if (m_GameState[dd])
        {
            delete m_GameState[dd];
            m_GameState[dd] = NULL;
        }
    }
    m_GameState.clear();
    return true;
}